const mongoose = require('mongoose');
const Cate = require('../models/cate.model');

//新增
exports.create = function(req, res, next) {
    const cate = new Cate(req.body);
    cate.save().then((data) => {
        res.json(data);
    });
};
//更新
exports.update = function(req, res, next) {
    var id = req.params.id;
    Cate.findByIdAndUpdate(id, { $set: req.body }, { new: false })
        .then(data => {
            res.json(data); //这里是修改前的数据，不是修改后的数据
        })
};
//删除

//查询  递归函数
function reverseTree(data, pid) {
    var result = [],
        temp;
    var data = JSON.parse(JSON.stringify(data)); //将数据模型的对象转成字符串，再将字符串转成Js.object
    //用json是因为模型内是没有children这个属性节点的，但后期又要加入children属性，所以这种转换后期就可以随意加
    for (var i in data) {
        if (data[i].parentId == pid) {
            result.push(data[i]);
            temp = reverseTree(data, data[i]._id);
            if (temp.length > 0) {
                data[i].children = temp;
            }
        }

    }
    return result;
}

exports.list = function(req, res, next) {
    var type = req.params.type || 1;
    Cate.find({ type: type }).then(data => {
        // console.log(data); //返回一个数组  说明可以进行递归操作

        var rpTree = reverseTree(data, null);
        res.json(rpTree);
        // console.log(rpTree[0].children[0]);

    })

};
//通过id
exports.getData = function(req, res, next) {
    var id = req.params.id;
    Cate.findById(id).then(data => {
        res.json(data);
    })
}